package cn.tedu.sort;

import org.junit.Test;

public class ShellSort extends BasicSort {

    @Override
    protected void sort(Comparable[] arr) {
        int N = arr.length;
        int h = 1;
        while (h < N / 3)
            h = 3 * h + 1; //1, 4, 13, 40, 121, 364, 1093, ...
        while (h >= 1) {
            // 将数组变为h有序
            for (int i = h; i < N; i++) {
                // 将arr[i]插入到arr[i - h], arr[i - 2 * h], arr[i - 3 * h]... 之中
                for (int j = i; j >= h && less(arr[j], arr[j - h]); j -= h)
                    exch(arr, j, j - h);
            }
            h = h / 3;
        }
    }

    @Test
    public void test() {
        generateNumbers();
        sort(numbers);
        show();
    }
}
